package itdfq.dfq.Structural_Patterns.qiaojie;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.File;

/**
 * @Author GocChin
 * @Date 2021/3/20 12:42
 * @Blog: itdfq.com
 * @QQ: 909256107
 */
public class XMLUtil {
    public static Object getBean(String args){
        try{
            DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = dFactory.newDocumentBuilder();
            Document doc;
            doc = builder.parse(new File("src/itdfq/dfq/Structural_Patterns/qiaojie/config.xml"));
            NodeList nl = null;
            Node classNode = null;
            String cName = null;
            nl = doc.getElementsByTagName("className");
            if (args.equals("color")){
                classNode = nl.item(0).getFirstChild();
            }else if(args.equals("car"))
            {
                classNode=nl.item(1).getFirstChild();
            }
            cName = classNode.getNodeValue();
            Class c = Class.forName(cName);
            Object obj =c.newInstance();
            return obj;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
